package edu.pku.sei.gefcommon.descriptor.core;

import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;

public class PropertyDescriptorAdapter implements IPropertySource {
	public Object hostSource;
	
	@Override
	public String toString() {
		return hostSource.toString();
	}


	public PropertyDescriptorAdapter(Object host){
		hostSource = host;
	}


	public IPropertyDescriptor[] getPropertyDescriptors() {
		if(isPrimitive(hostSource.getClass())) {
			return PropertyDescriptorFactory.getPrimitiveTypeDescriptor(hostSource.getClass());
		} else {
			return PropertyDescriptorFactory.getPropertyDescriptors(hostSource);
		}
	}

	public Object getPropertyValue(Object id) {
		if(isPrimitive(hostSource.getClass())){
			return PropertyDescriptorFactory.getPrimitiveTypeValue(hostSource);
		} else {
			return PropertyDescriptorFactory.getPropertyValue(hostSource, id);
		}
	}
	
	public void setPropertyValue(Object id, Object value) {
		if(isPrimitive(hostSource.getClass())){
			PropertyDescriptorFactory.setPrimitiveTypeValue(hostSource,value);
		} else {
			PropertyDescriptorFactory.setPropertyValue(hostSource, id, value);
		}
	}

	public Object getEditableValue() {return hostSource;}
	public boolean isPropertySet(Object arg0) {return true;}
	public void resetPropertyValue(Object arg0) {}
	
	static public boolean isPrimitive(Class type){
		if(type==String.class)
			return true;
		else if(type==int.class||type==Integer.class)
			return true;
		else if(type==boolean.class||type==Boolean.class)
			return true;
		else if(type==double.class||type==Double.class)
			return true;
		else if(type==float.class||type==Float.class)
			return true;
		return false;
	}
}
